package com.xiaodou.mjf.infrastructure.generate;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.Properties;

import org.apache.commons.io.FilenameUtils;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.xiaodou.mjf.infrastructure.common.utils.FileUtils;
import com.xiaodou.mjf.infrastructure.common.utils.PropertiesLoader;
import com.xiaodou.mjf.infrastructure.common.utils.StringUtils;

/**
 * Created by Rocky on 15/1/3.
 */
public abstract class ParentGenerator {

    Logger logger = LoggerFactory.getLogger(getClass());

    String extension = FilenameUtils.EXTENSION_SEPARATOR_STR;
    Properties props = null;
    String basePackage = null;
    String templatePath = null;

    boolean OVERWRITE = false;

    public abstract void generate(XSSFWorkbook workbook,File dest) throws Exception;

    public void init(){
        props = new PropertiesLoader("generate.properties").getProperties();
        basePackage = props.getProperty("generator.base");
        templatePath = props.getProperty("generator.template");

    }

    public void write(String content,String path){
        if(OVERWRITE){
            FileUtils.deleteFile(path);
        }
        try{
            if(FileUtils.createFile(path)){
                FileWriter writer = new FileWriter(path,true);
                BufferedWriter bufferedWriter = new BufferedWriter(writer);
                bufferedWriter.write(content);
                bufferedWriter.close();
            }else{
                logger.debug("{}已存在，生成失败", StringUtils.substringAfterLast(path,File.separator));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
